﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace ServiceLibrary.App_Code
{
    public class JudgeMessage
    {
        /// <summary>
        /// 判断  身份证号码  的合法性
        /// </summary>
        /// <param name="IDCard"></param>
        /// <returns></returns>
        public bool IsLegalIDCard(String IDCard)
        {
            Regex myReg = new Regex(@"d{18}|d{15}");
            if (IDCard.ToString() == "" || IDCard.ToString() == null)
            {
                return true;
            }
            else
            {
                if (myReg.IsMatch(IDCard))
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 判断  邮箱  的合法性
        /// </summary>
        /// <param name="EMail"></param>
        /// <returns></returns>
        public bool IsLegalEMail(String EMail)
        {
            Regex myReg = new Regex(@"w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*");
            if (EMail.ToString() == "" || EMail.ToString() == null)
            {
                return true;
            }
            else
            {
                if (myReg.IsMatch(EMail))
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 判断  邮政编码  的合法性
        /// </summary>
        /// <param name="PostCode"></param>
        /// <returns></returns>
        public bool IsLegalPostalCode(String PostCode)
        {
            Regex myReg = new Regex(@"d{6}");
            if (PostCode.ToString() == "" || PostCode.ToString() == null)
            {
                return true;
            }
            else
            {
                if (myReg.IsMatch(PostCode))
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 判断  固定电话号码  的合法性
        /// 匹配形式如:0511-4405222 或者021-87888822 或者 021-44055520-555 或者 (0511)4405222 
        /// </summary>
        /// <param name="Telephone"></param>
        /// <returns></returns>
        public bool IsLegalTelephone(String Telephone)
        {
            Regex myReg = new Regex(@"^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$");
            if (Telephone.ToString() == "" || Telephone.ToString() == null)
            {
                return true;
            }
            else
            {
                if (myReg.IsMatch(Telephone))
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 判断  手机号码  的合法性
        /// </summary>
        /// <param name="MobilePhone"></param>
        /// <returns></returns>
        public bool IsLegalMobilePhone(String MobilePhone)
        {
            Regex myReg = new Regex(@"^[1]+[3,5,8]+\d{9}");
            if (MobilePhone.ToString() == "" || MobilePhone.ToString() == null)
            {
                return true;
            }
            else
            {
                if (myReg.IsMatch(MobilePhone))
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 判断  网址  的合法性
        /// </summary>
        /// <param name="Link"></param>
        /// <returns></returns>
        public bool IsLegalLink(String Link)
        {
            Regex myReg = new Regex(@"^http://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?$");
            if (Link.ToString() == "" || Link.ToString() == null)
            {
                return true;
            }
            else
            {
                if (myReg.IsMatch(Link))
                    return true;
                else
                    return false;
            }
        }
        /// <summary>
        /// 判断用户名是否合法
        /// 用户名
        /// 合法：  TRUE
        /// 不合法：False
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public bool IsLegalName(String Name)
        {
            bool IsSuccess = false;
            Regex myReg = new Regex(@"^(?:[\u4e00-\u9fa5]*\w*\s*)+$"); //屏蔽非法字符 
            if (myReg.IsMatch(Name))
                IsSuccess = true;
            else
                IsSuccess = false;
            return IsSuccess;
        }
        /// <summary>
        /// 判断密码是否正确
        /// 密码：   长度在6～18之间，只能包含字符、数字和下划线";
        /// 合法：  TRUE
        /// 不合法：False
        /// </summary>
        /// <param name="Password"></param>
        /// <returns></returns>
        public bool IsLegalPassword(String Password)
        {
            bool IsSuccess = false;
            Regex myReg = new Regex(@"^[a-zA-Z]\w{5,17}$");
            if (myReg.IsMatch(Password))
                IsSuccess = true;
            else
                IsSuccess = false;
            return IsSuccess;
        }
        /// <summary>
        /// 权限的合法性
        /// 权限：0-6
        /// 合法：  TRUE
        /// 不合法：False
        /// </summary>
        /// <returns></returns>
        public bool IsLegalPower(String Power)
        {
            bool IsSuccess = false;
            Regex myReg = new Regex(@"^[0-6]*$");
            if (myReg.IsMatch(Power))
                IsSuccess = true;
            else
                IsSuccess = false;
            return IsSuccess;
        }
        /// <summary>
        /// 验证用户名或是密码中是否有非法字符
        /// 如果含有非法字符  TRUE
        ///             没有  FALSE
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public bool CheckCharacter(String Str)
        {
            bool IsSuccess = false;          // 使用for循环,遍历字符串中的字符
            for (int i = 0; i < (int)Str.Length; i++)
            {
                // 使用Substring()方法获取当前字符串中的每一个字符，并且将获取的字符赋值给变量name中
                String name = Str.Substring(i, 1);
                if (name == "'" || name == "%" || name == "<" || name == ">" || name == "&" || name == "|")
                {
                    IsSuccess = true;        // 将bool类型的变量赋一个值true
                    break;                   // 跳出for循环
                }
            }
            return IsSuccess;
        }
    }
}
